package com.bwhx.eruler.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bwhx.eruler.bean.Buyer;
import com.bwhx.eruler.bean.FleeRuler;
import com.bwhx.eruler.bean.Ruler;
import com.bwhx.eruler.bean.User;
import com.bwhx.eruler.bean.Version;
import com.bwhx.eruler.service.BuyerSdkService;
import com.bwhx.eruler.service.BuyerService;
import com.bwhx.eruler.service.FleeRulerService;
import com.bwhx.eruler.service.RulerService;
import com.bwhx.eruler.service.UserService;
import com.bwhx.eruler.service.VersionService;

/**
 * 电子尺仓库管理，销售管理
 * @author Cuilitang
 *
 */
@Controller
public class RulerController extends BaseController {
	
	@Autowired
	VersionService verService;
	
	@Autowired
	RulerService rulerService;
	
 
	
	@Autowired
	UserService userService;
	
	@Autowired
	FleeRulerService fleeRulerService;
	
	@Autowired
	BuyerService buyerService;
	
	
	
	
	@SuppressWarnings("unchecked")
	@ResponseBody
	@RequestMapping(value = "/RulerValidate.ashx",method={RequestMethod.POST})
	 public Map getetSCanDataInfo(HttpServletRequest request) {
		
		Map map = new HashMap();
		
		Object mac = request.getAttribute("mac");
		Object mid = request.getAttribute("mid");
		try{
			if(mac!=null&&mid!=null){
				

				 Ruler r= rulerService.findByMAC((String)mac);
				 if(r==null){
					 map.put("meg", "未注册产品");
					 map.put("status", "0");
				 }else{
					 
					 if(r.getrState()==0){
						 map.put("meg", "产品未出库！");
						 map.put("status", "0");
					 }
					 //检测E尺是否正常流出
					 if(1!=r.getrState()&&Integer.parseInt(mid.toString())!=-1){
						//非正常流出
						 //根据Mid获取量体师信息
						 //获取尺子信息
						 //将信息插入到非法流出表
						 User user = userService.find(Integer.parseInt(mid.toString()));
						 FleeRuler fleeRuler = new FleeRuler();
						 fleeRuler.setfIsprocess(0);
						 fleeRuler.setfMname(user.getUsername());
						 fleeRuler.setfMphone(user.getMobile());
						 fleeRuler.setfMregtime(user.getRegdatetime());
						 fleeRuler.setfRmac(r.getrMac());
						 fleeRuler.setfRsn(r.getRsn());
						 fleeRuler.setfRstatus(r.getrState());
						 fleeRuler.setfTime(System.currentTimeMillis()/1000);
						 fleeRulerService.insert(fleeRuler);
						 
					 }
					//基础版，高定班，团装版
					map.put("version", r.getrVersion()); 
					
					//购买主体企业码
					if(r.getrBuyerId()!=null){
						
						Buyer buyer = buyerService.get(r.getrBuyerId());
						map.put("buyercode", buyer==null?null:buyer.getbCode());
					}else{
						map.put("buyercode", null);
					}
					
					
					//app版本
					List<Version> verListAll = verService.verListAll();
					for (Version version : verListAll) {
						map.put(version.getvName(), version.getvValue());
					}
					
					//存储用户和E尺的关联关系
					Integer midInteger = Integer.parseInt(mid.toString());
					if(midInteger.intValue()!=-1){					
						Map<String,Integer> paramMap = new HashMap<>();
						paramMap.put("mid", midInteger);
						paramMap.put("rid", r.getrId());
						
						rulerService.insertRulerUser(paramMap);	
					}
					
					map.put("meg", "正品");
					map.put("status", "1");
				 }
			}else{
				map.put("meg", "缺少参数");
				 map.put("status", "0");
			}
			
		}catch(Exception e){
			log.error(getTrace(e));
			map.put("meg", "系统出错");
			map.put("status", "-1");
		}
	    return map;  
	  }
	
	
 

 



	@SuppressWarnings("unchecked")
	@ResponseBody
	@RequestMapping(value = "/sdk.RulerValidate.ashx",method={RequestMethod.POST})
	 public Map RulerValidate(HttpServletRequest request) {
		
		Map map = new HashMap();
		
		Object mac = request.getAttribute("mac");
		Object sdkId = request.getAttribute("sdkId");
		try{
			if(mac!=null&&sdkId!=null){
				

				 Ruler r= rulerService.findByMAC((String)mac);
				 if(r==null){
					 map.put("meg", "未注册产品");
					 map.put("status", "0");
				 }else{
					 //检测E尺是否正常流出
					 if(1!=r.getrState()){
						//非正常流出
						 //根据Mid获取量体师信息
						 //获取尺子信息
						 //将信息插入到非法流出表
						 Buyer buyer = buyerService.getBySDKID(sdkId.toString());
						 if (buyer!=null) {
							FleeRuler fleeRuler = new FleeRuler();
							fleeRuler.setfIsprocess(0);
							fleeRuler.setfMname(buyer.getbName() + "SDK客户端");
							fleeRuler.setfRmac(r.getrMac());
							fleeRuler.setfRsn(r.getRsn());
							fleeRuler.setfRstatus(r.getrState());
							fleeRuler.setfTime(System.currentTimeMillis() / 1000);
							fleeRulerService.insert(fleeRuler);
						}else{
							 map.put("meg", "E尺未出库，并且sdkId有误");
							 map.put("status", "0");
							 return map;  
						}
						 
					 }
					//基础版，高定班，团装版
					map.put("version", r.getrVersion()); 
					

					//购买主体企业码
					if(r.getrBuyerId()!=null){
						
						Buyer buyer = buyerService.get(r.getrBuyerId());
 						map.put("buyercode", buyer==null?null:buyer.getbCode());
 						
					}else{
						map.put("buyercode", null);
					}
					
					//存储SDK和E尺的关联关系
					String sdkIdInteger = sdkId.toString();
					Map<String,Object> paramMap = new HashMap<>();
					paramMap.put("sdkId", sdkIdInteger);
					paramMap.put("rid", r.getrId());
					paramMap.put("dateTime", System.currentTimeMillis()/1000);
					rulerService.insertRulerSdk(paramMap);
					map.put("meg", "正品");
					map.put("status", "1");
				 }
			}else{
				map.put("meg", "缺少参数");
				map.put("status", "0");
			}
			
		}catch(Exception e){
			log.error(getTrace(e));
			map.put("meg", "系统出错");
			map.put("status", "-1");
		}
	    return map;  
	  }
	
	
	 
}
